package com.reggie.controller.interceptor;


import com.alibaba.fastjson.JSON;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.entity.Employee;
import com.reggie.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();

        log.info("请求路径：" + request.getRequestURI());
        Long employee = (Long) session.getAttribute("employee");
        if(employee != null){
            BaseContext.setCurrentId(employee);
            return true;
        }

        Long user = (Long) session.getAttribute("user");
//        Long user = 1626229858415685634L;
        if(user != null){
            BaseContext.setCurrentId(user);
            return true;
        }
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return false;
    }
}
